package com.company.project.configurer;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * @Author: zhaoqi
 * @Date: 11/5/2019 9:26 PM
 * @Version 1.0
 */

@Intercepts({
        @Signature(type = StatementHandler.class,method = "parameterize",args = java.sql.Statement.class)
})
public class zhaoqiIntercepter implements Interceptor {
    /**
     * intercept:拦截
     *      拦截目标对象的方法的执行
     * @param invocation
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("MyFirstPlugin-----------------interceptor:"+this.toString()+"----"+invocation.getMethod());
        //执行目标方法
        Object proceed = invocation.proceed();
        //返回执行后的返回值
        return proceed;
    }

    /**
     * plugin:包装目标对象——包装：为目标对象创建一个代理类
     * @param target
     * @return
     */
    @Override
    public Object plugin(Object target) {
        System.out.println("MyFirstPlugin....plugin:mybatis将要包装的对象"+this.toString()+"----"+target);
        //我们可以借助Plugin的wrap方法来使用当前的intercept包装我们目标对象
        Object wrap = Plugin.wrap(target, this);
        //返回为当前target创建好的动态代理

        return wrap;
    }

    /**
     * setProperties:将插件注册时的property属性设置进来
     * @param properties
     */
    @Override
    public void setProperties(Properties properties) {
        System.out.println("插件配置------------的信息"+this+"----"+properties);
    }


    @Test
    public void test(){
        System.out.println(1321);
    }

    @Before
    public void test1(){
        System.out.println("方法前");
    }

    @After
    public void test2(){
        System.out.println("方法后");
    }


}
